import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

/**
 * 
 * A JPanel allows the user to load from a saved game or create a new User.
 *
 */
public class LoginPanel extends JPanel implements ActionListener
{
	JComboBox users;
	JButton go;
	JButton delete; 
	JButton createNewUser;
	ArrayList<String> names;
	
	public LoginPanel()
	{
		names= new ArrayList<String>();
		setNames();
		users= new JComboBox(names.toArray());
		go= new JButton("Go");
		go.setMnemonic(KeyEvent.VK_G);
		createNewUser= new JButton("Create a New User");
		createNewUser.setMnemonic(KeyEvent.VK_C);
		delete= new JButton("Delete User");
		delete.setMnemonic(KeyEvent.VK_D);
		go.addActionListener(this);
		createNewUser.addActionListener(this);
		delete.addActionListener(this);
		add(users);
		add(createNewUser);
		add(delete);
		add(go);
	}
	
	/**
	 * Replaces this panel with a new SettingsPanel or CreateUserPanel
	 * based on the users input.
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getActionCommand()==createNewUser.getActionCommand())
		{
			String[] sNames= new String[names.size()];
			for(int k=0; k<names.size(); k++)
			{
				sNames[k]= names.get(k);
			}
			replacePanel(new CreateUserPanel(sNames));
		}
		else if(e.getActionCommand()==delete.getActionCommand())
		{
			int choice= JOptionPane.showConfirmDialog(getTopLevelAncestor(),
										  		"Are You Shure?", 
										  		"Delete User", JOptionPane.YES_NO_OPTION);
			if(choice==JOptionPane.YES_OPTION&&users.getSelectedItem()!=null)
			{
				File file= new File(users.getSelectedItem()+".dat");
				file.delete();
				names.remove(users.getSelectedItem());
				users.removeItem(users.getSelectedItem());
			}
		}
		else // go button
		{
			if(users.getSelectedItem()!=null&&users.getSelectedItem()!="")
			{
				try
				{
					if(Global.currentUser!=null) Global.currentUser.save();
					Global.currentUser= User.load((String)users.getSelectedItem());
					SettingsPanel newPanel= new SettingsPanel();
					replacePanel(newPanel);
				}
				catch(IOException ex)
				{
					ex.printStackTrace();
					return;
				}
			}
		}
	}
	
	/**
	 * Gets the saved games in the current directory and updates the JComboBox
	 * with the options.
	 */
	private void setNames()
	{
		final int CHARS_IN_DIR= 4;
		File dir= new File(System.getProperty("user.dir"));
		String[] files= dir.list();
		for(int k=0; k<files.length; k++)
		{
			if(files[k].endsWith(".dat"))
			{
				String file= files[k];
				char[] temp= new char[file.length()-CHARS_IN_DIR];
				for(int i=0; i<file.length()-CHARS_IN_DIR; i++)
				{
					temp[i]= file.charAt(i);
				}
				names.add(new String(temp));
			}
		}
	}
	
	/**
	 * Replaces this with a new JPanel.
	 * 
	 * @param newPanel the JPanel to replace this one.
	 */
	private void replacePanel(JPanel newPanel)
	{
		setVisible(false);
		Component ancestor= this.getTopLevelAncestor();
		if(Global.isApplet()) 
		{
			JApplet applet= (JApplet)(ancestor);
			applet.setContentPane(newPanel);
		}
		else 
		{
			JFrame frame= (JFrame)(ancestor);
			frame.setContentPane(newPanel);
			frame.pack();
		}
	}
}
